﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUS.ISS.ARS.BusinessEntity;
using NUS.ISS.ARS.BusinessEntity.ViewModels;
using NUS.ISS.ARS.Common;
using System.Data.Entity;

namespace NUS.ISS.ARS.BusinessComponent
{
    public class FlightScheduleBC:BaseBC
    {
        public ICollection<FlightSchedule> GetAllFlightSchedules()
        {
            try
            {
                using (var ent = new Entities())
                {
                    return ent.FlightSchedules.ToList();
                }
            }
            catch (Exception ex)
            {
                //Todo: Log Exception
            }
            return null;
        }

        public IEnumerable<Airport> IEnumGetAllAirport()
        {
            using (var ent = new Entities())
            {
                return ent.Airports.ToList();
            }
        }

        public FlightScheduleView GetAllFlightScheduleById(long id)
        {
            FlightScheduleView flightScheduleView = null;
            using (var ent = new Entities())
            {
                flightScheduleView = ent.FlightSchedules.Where(a => a.FlightScheduleId==id && a.IsDeleted == false)
                                             .Include(a => a.Aircraft)
                        .Select(t => new FlightScheduleView()
                        {
                            FlightNumber = t.FlightNumber,
                            FlightScheduleId = t.FlightScheduleId,
                            FromAirport = t.Airport.AirportName,
                            ToAirport = t.Airport1.AirportName,
                            ChildFare = t.ChildFare,
                            AdultFare = t.AdultFare,
                            InfantFare = t.InfantFare,
                            DepartureTime = t.DepartureTime,
                            ArrivalTime = t.ArrivalTime,
                            FromCity = t.Airport.City.CityName,
                            ToCity = t.Airport1.City.CityName,
                            isDeleted = t.IsDeleted,
                            AirplaneId = t.AircraftId,
                            AirplaneModel = t.Aircraft.Model,
                            FromAirportId = t.FromAirportId,    
                            ToAirportId = t.ToAirportId  
                        }).OrderBy(a => a.FlightNumber).FirstOrDefault();
            }

            return flightScheduleView;        
        
        }


        public IEnumerable<FlightScheduleView> GetFlightSchedulesByFlightNumberAirport(string flightNumber, string airportFrom, string airportTo)
        {
            IEnumerable<FlightScheduleView> flightScheduleView = null;

            using (var ent = new Entities())
            {
                flightScheduleView = ent.FlightSchedules.Where(a => a.FlightNumber.Contains(flightNumber)
                                        && a.Airport.AirportName.Contains(airportFrom)
                                        && a.Airport1.AirportName.Contains(airportTo) && a.IsDeleted == false)
                                         .Include(a => a.Aircraft)
                        .Select(t => new FlightScheduleView()
                        {
                            FlightNumber = t.FlightNumber,
                            FlightScheduleId = t.FlightScheduleId,
                            FromAirport = t.Airport.AirportName,
                            ToAirport = t.Airport1.AirportName,
                            ChildFare = t.ChildFare,
                            AdultFare = t.AdultFare,
                            InfantFare = t.InfantFare,
                            DepartureTime = t.DepartureTime,
                            ArrivalTime = t.ArrivalTime,
                            FromCity = t.Airport.City.CityName,
                            ToCity = t.Airport1.City.CityName,
                            isDeleted = t.IsDeleted,
                            AirplaneId = t.AircraftId,
                            AirplaneModel = t.Aircraft.Model,
                            FromAirportId = t.FromAirportId,
                            ToAirportId = t.ToAirportId  
                        }).OrderBy(a => a.FlightNumber).ToList();
            }

            return flightScheduleView;
        }
        public IEnumerable<FlightScheduleView> GetFlightSchedulesByFlightDateFlightNumberCityFromCityTo(DateTime flightdate, string flightNumber, string cityFrom, string cityTo)
        {
            IEnumerable<FlightScheduleView> flightScheduleView = null;

            using (var ent = new Entities())
            {
                flightScheduleView = ent.FlightSchedules.Where(a => a.FlightNumber.Contains(flightNumber)
                                        && a.Airport.City.CityName.Contains(cityFrom)
                                        && a.Airport1.City.CityName.Contains(cityTo) && a.IsDeleted == false)
                                         .Include(a => a.Aircraft)
                        .Select(t => new FlightScheduleView()
                        {
                            FlightNumber = t.FlightNumber,
                            FlightScheduleId = t.FlightScheduleId,
                            FromAirport = t.Airport.AirportName,
                            ToAirport = t.Airport1.AirportName,
                            ChildFare = t.ChildFare,
                            AdultFare = t.AdultFare,
                            InfantFare = t.InfantFare,
                            DepartureTime = t.DepartureTime,
                            ArrivalTime = t.ArrivalTime,
                            FromCity = t.Airport.City.CityName,
                            ToCity = t.Airport1.City.CityName,
                            isDeleted = t.IsDeleted,
                            AirplaneId = t.AircraftId,
                            AirplaneModel = t.Aircraft.Model,
                            FromAirportId = t.FromAirportId,
                            ToAirportId = t.ToAirportId  
                        }).OrderBy(a => a.FlightNumber).ToList();
            }

            return flightScheduleView;
        }
        public IEnumerable<FlightScheduleView> IEnumGetAllFlightSchedules()
        {
            IEnumerable<FlightScheduleView> flightScheduleView = null;
            using (var ent = new Entities())
            {
                flightScheduleView = ent.FlightSchedules.Include(a => a.Aircraft).Where(a => a.IsDeleted == false)
                    .Select(t => new FlightScheduleView()                       
                {
                    FlightNumber = t.FlightNumber,
                    FlightScheduleId = t.FlightScheduleId,
                    FromAirport = t.Airport.AirportName,
                    ToAirport = t.Airport1.AirportName,
                    ChildFare = t.ChildFare,
                    AdultFare = t.AdultFare,
                    InfantFare = t.InfantFare,
                    DepartureTime = t.DepartureTime,
                    ArrivalTime = t.ArrivalTime,
                    FromCity = t.Airport.City.CityName,
                    ToCity = t.Airport1.City.CityName,
                    isDeleted = t.IsDeleted,
                    AirplaneId = t.AircraftId,
                    AirplaneModel = t.Aircraft.Model,
                    FromAirportId = t.FromAirportId,
                    ToAirportId = t.ToAirportId  
                }).OrderBy(a => a.FlightNumber).ToList();

                return flightScheduleView;
            }
        }
        bool _valid = true;
        public bool Valid
        {
            get { return _valid; }
        }
        private void SetError(string msg)
        {
            _errorMessage += msg + Environment.NewLine;
            _valid = false;
        }
        string _errorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return _errorMessage; }
        }
        public SaveResult SaveSchedule(FlightScheduleView schedule)
        {
            FlightSchedule flightschedule = new FlightSchedule();
            flightschedule.FlightScheduleId = schedule.FlightScheduleId;
            flightschedule.FlightNumber = schedule.FlightNumber;
            flightschedule.FromAirportId = schedule.FromAirportId;
            flightschedule.ToAirportId = schedule.ToAirportId;
            flightschedule.AircraftId = schedule.AirplaneId;
            flightschedule.DepartureTime = schedule.DepartureTime;
            flightschedule.ArrivalTime = schedule.ArrivalTime;
            flightschedule.AdultFare = schedule.AdultFare;
            flightschedule.ChildFare = schedule.ChildFare;
            flightschedule.IsDeleted = schedule.isDeleted;
            flightschedule.InfantFare = schedule.InfantFare;

            if (flightschedule.IsValid)
            {
                try
                {
                    using (var ent = new Entities())
                    {
                        ent.FlightSchedules.Add(flightschedule);
                        ent.SaveChanges();
                    }
                }
                catch (Exception)
                {
                    //Todo: Log Exception
                    SetError("Exception occurred.");

                    return new SaveResult()
                    {
                        Status = Constants.SaveStatus.Failed,
                        Message = ErrorMessage
                    };
                }

                return new SaveResult()
                {
                    Status = Constants.SaveStatus.Success,
                    Message = ""
                };
            }
            else
            {
                return new SaveResult
                {
                    Status = Constants.SaveStatus.Success,
                    Message = flightschedule.ErrorMessage
                };
            }
        }
        public SaveResult SaveChangedSchedule(FlightScheduleView schedule)
        {
            /*
            if (schedule == null)
            {
                SetError("Flight Schedules count cannot be null or zero.");
                return Constants.SaveStatus.Failed;
            }
            if (schedule.FlightScheduleId == 0 || schedule.FlightScheduleId == null) 
            {
                SetError("Flight Schedules count cannot be null or zero.");
                return Constants.SaveStatus.Failed;
            }*/
            try{
                 using (var ent = new Entities())
                  {
                      var flightSchedule = ent.FlightSchedules.FirstOrDefault(b => b.FlightScheduleId == schedule.FlightScheduleId);
                        if (flightSchedule != null)
                        {
                            flightSchedule.FlightScheduleId = schedule.FlightScheduleId;
                            flightSchedule.FlightNumber = schedule.FlightNumber;
                            flightSchedule.FromAirportId = schedule.FromAirportId;
                            flightSchedule.ToAirportId = schedule.ToAirportId;
                            flightSchedule.AircraftId = schedule.AirplaneId;
                            flightSchedule.DepartureTime = schedule.DepartureTime;
                            flightSchedule.ArrivalTime = schedule.ArrivalTime;
                            flightSchedule.AdultFare = schedule.AdultFare;
                            flightSchedule.ChildFare = schedule.ChildFare;
                            flightSchedule.InfantFare = schedule.InfantFare;
                            flightSchedule.IsDeleted = schedule.isDeleted;

                            if (flightSchedule.IsValid)
                            {
                                ent.SaveChanges();
                            }
                            else
                            {
                                return new SaveResult()
                                {
                                    Status = Constants.SaveStatus.Failed,
                                    Message = flightSchedule.ErrorMessage
                                };
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                //TODO: log error
                SetError("Exception occurred.");
                return new SaveResult()
                {
                    Status = Constants.SaveStatus.Failed,
                    Message = ErrorMessage
                };
            }

            return new SaveResult()
            {
                Status = Constants.SaveStatus.Success,
                Message = ""
            };
        }
    }
}
